package gsp420;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements MouseListener, Runnable, ActionListener, KeyListener
{
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() instanceof JButton)
		{
			m_location.y = 0;
		}
		else if(e.getSource() instanceof JTextField)
		{
			JTextField jtf = (JTextField)e.getSource();
			m_text = jtf.getText();
		}
	}
	/**
	 * @deprecated
	 * @param args
	 */
	public static void main(String[] args)
	{
		for(int i = 0; i < args.length; ++i)
		{
			System.out.println("["+args[i]+"]");
		}
		JFrame jf = new JFrame("Game Panel Main");
		JPanel outerPanel = new JPanel(new BorderLayout());
		GamePanel gp = new GamePanel(null);
		outerPanel.add(gp, BorderLayout.CENTER);
		JTextField jtf = new JTextField();
		outerPanel.add(jtf, BorderLayout.SOUTH);
		JLabel jlbl = new JLabel("label");
		JLabel jlbl2 = new JLabel("label2");
		JLabel jlbl3 = new JLabel("label3");
		JLabel jlbl4 = new JLabel("label4");
		JButton jbtn = new JButton("Press Me!");
		outerPanel.add(jbtn, BorderLayout.NORTH);
		JPanel innerPanel = new JPanel(new GridLayout(3,2));
		innerPanel.add(jlbl);
		innerPanel.add(jlbl2);
		innerPanel.add(jlbl3);
		innerPanel.add(jlbl4);
		outerPanel.add(innerPanel, BorderLayout.EAST);
		jtf.addActionListener(gp);
		jbtn.addActionListener(gp);
		jf.add(outerPanel);
		jf.setSize(200,200);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		V2D xy = new V2D();
		System.out.println(xy);
		int[] blarg = new int[100];
		int[] a = blarg;
		blarg = null;
		a = null;
		System.gc();

		// example of Java reflection
		try
		{
			Class c = Class.forName("gsp420.GamePanel");
			Method[] m2 = c.getMethods();
			for(int i = 0; i < m2.length; ++i)
			{
				Method m = m2[i];
				Type[] args2 = m.getGenericParameterTypes();
				System.out.print(m2[i].getGenericReturnType()+" "+m2[i].getName()+" (");
				for(int a1 = 0; a1 < args2.length; ++a1)
				{
					System.out.print(args2[a1].toString());
				}
				System.out.println(")");
			}
		}catch(Exception e)
		{
			
		}
	}
	public void omgwtfbbq()
	{
		System.out.println("LOOK I AM RUNNING!!!");
	}
	/**
	 * changes the <i>location</i> <u>of where</u> <code>"Hello World"</code> <b>appears</b>
	 * <li>item 1
	 * <li>item 2
	 * <table border = 2>
	 * 	<tr><td>element1</td><td>element2</td></tr>
	 * 	<tr><td>element3</td><td>element3</td></tr>
	 * </table>
	 * <marquee>OMG</marquee>
	 * <img src = "http://codegiraffe.com/images/icon_colored.png">
	 */
	V2D m_location = new V2D();
	V2D m_dir = new V2D();
	
	/** font used to draw text in the game */
	Font m_font;
	
	/** game icon */
	Image m_icon;
	
	/** the message */
	String m_text;
	
	public GamePanel(JApplet applet)
	{
		m_text = "hello world";
		m_font = new Font("Chiller", Font.ITALIC, 24);
		if(applet == null)
		{
			m_icon = Toolkit.getDefaultToolkit().createImage("img.png");
		}
		else
		{
			try{
			m_icon = applet.getImage(
					new URL("http://icons.iconarchive.com/icons/famfamfam/silk/16/arrow-refresh-small-icon.png"));
//					applet.getDocumentBase(), "img.png");
			}catch(Exception e){}
		}
		addMouseListener(this);
		addKeyListener(this);
		Thread t = new Thread(this);
		t.start();
	}
	void f()
	{
		
	}
	protected void a(){}
	
	Graphics m_g;
	
	/**
	 * draws to the panel
	 * @param a_graphicsContext the graphics context
	 */
	public void paintComponent(Graphics a_graphicsContext)
	{
		// shows that the graphics context is a new instance almost each time paintComponent is called
//		if(m_g != a_graphicsContext)
//		{
//			m_g = a_graphicsContext;
//			System.out.println("new context "+m_g.hashCode());
//		}
		a_graphicsContext.setFont(m_font);
//		g.setColor(Color.WHITE);
//		g.fillRect(0, 0, getWidth(), getHeight());
//		g.setColor(Color.BLACK);
		super.paintComponent(a_graphicsContext);
		a_graphicsContext.drawString(m_text, 
				(int)m_location.x, (int)m_location.y);
		a_graphicsContext.drawImage(m_icon, (int)m_location.x, (int)m_location.y, null);
	}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	/**
	 * modifies the value of {@link #m_location}
	 */
	public void mousePressed(MouseEvent e)
	{
		requestFocus();	// you must get focus to get key events
		System.out.println("pressed "+e.getX()+" "+e.getY());
		this.m_location.set(e);
		repaint();
		m_dir.y = 0;
	}

	void update(){
		if(m_location.y >= getHeight()-m_icon.getHeight(null))
		{
			m_location.y = getHeight()-m_icon.getHeight(null);
			m_dir.y *= -0.6;
			if(Math.abs(m_dir.y) < .5)
				m_dir.y = 0;
		}
		else
		{
			m_dir.y += 0.1;
		}
		m_location.add(m_dir);
	}
	
	private boolean m_running;
	public void run()
	{
		m_running = true;
		while(m_running)
		{
			repaint();
			try{Thread.sleep(10);}catch(Exception e){}
			update();
		}
	}

	public void keyPressed(KeyEvent e)
	{
		m_text = Character.toString(e.getKeyChar());
	}
	public void keyReleased(KeyEvent arg0){}
	public void keyTyped(KeyEvent arg0){}
}